<?php
namespace Base;

use Base\Exception\RmException;

abstract class BaseOrmObjectLists implements \arrayaccess
{
    protected $offset, $limit, $total, $filter, $where, $order, $lists, $dbo, $fields;

    function __construct()
    {
        $this->offset = 0;
        $this->limit = 20;
        $this->total = 0;
        $this->filter = '';
        $this->where = '';
        $this->order = '';
        $this->lists = array();
        $this->dbo = null;
        $this->fields = '*';
    }

    //--------------------------------------------------------------------------------------------//
    //	functions to implemet for ArrayAccess
    //--------------------------------------------------------------------------------------------//
    public final function offsetSet($offset, $value)
    {
        $this->{$offset} = $value;
    }

    public final function offsetExists($offset)
    {
        return isset($this->{$offset});
    }

    public final function offsetUnset($offset)
    {
        unset($this->{$offset});
    }

    public final function offsetGet($offset)
    {
        return isset($this->{$offset}) ? $this->{$offset} : null;
    }
    //--------------------------------------------------------------------------------------------//

    /**
     *
     */
    public function load()
    {
        /***********************************/
        $this->lists = array();
        /***********************************/
        if (strlen($this->where) && strlen($this->filter)) {
            $where = $this->where . ' AND ' . $this->filter;
        } else {
            $where = $this->where . $this->filter;
        }
        $total = $this->dbo->fetchRowCount('*', trim($where));
        $this->total = $total['num'];
        $lists = $this->dbo->fetchAll($this->fields, $this->order, trim($where), $this->limit, $this->offset);
        foreach ($lists as $list) {
            $obj = $this->newObj();
            $obj->__init($list);
            if ($this->key_by && isset($list[$this->key_by])) {
                $this->lists[$list[$this->key_by]] = $obj;
            } else {
                $this->lists[] = $obj;
            }
            //array_push($this->lists, $list);
        }

    }

    public function loadAll()
    {
        /***********************************/
        $this->lists = array();
        /***********************************/
        $where = $this->where;
        $total = $this->dbo->fetchRowCount('*', trim($where));
        $this->total = $total['num'];
        $lists = $this->dbo->fetchAll($this->fields, $this->order, trim($where), $this->limit, $this->offset);
        foreach ($lists as $list) {
            $obj = $this->newObj();
            $obj->__init($list);
            if ($this->key_by && isset($list[$this->key_by])) {
                $this->lists[$list[$this->key_by]] = $obj;
            } else {
                $this->lists[] = $obj;
            }
            //array_push($this->lists, $list);
        }

    }

    protected abstract function newObj();
}